package com.shengqiang.cloud.oauth2.controller;

import com.shengqiang.cloud.oauth2.model.Client;
import com.shengqiang.cloud.oauth2.model.ResponseMsg;
import com.shengqiang.cloud.oauth2.service.OAuthClientService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/12/9.
 */
@Controller
@RequestMapping("/oauth2Client")
public class OauthClientController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private OAuthClientService service;

    @ResponseBody
    @RequestMapping(value = "/author", method = RequestMethod.GET)
    public String author() {
        String value = redisTemplate.opsForValue().get("test").toString();
        return value;
    }

    @ApiOperation(value = "注册客户的信息", notes = "")
    @ApiImplicitParam(name = "client", value = "注册的客户端实体", dataType = "Client", required = true)
    @ResponseBody
    @RequestMapping(value = "/registerClient", method = RequestMethod.POST)
    public ResponseMsg<Client> registerClient(@RequestBody Client client) {
        ResponseMsg<Client> responseMsg = service.registerClient(client);
        return responseMsg;
    }

    @ApiOperation(value = "更新客户的信息", notes = "")
    @ApiImplicitParam(name = "client", value = "更新的客户端实体", dataType = "Client", required = true)
    @ResponseBody
    @RequestMapping(value = "/updateClient", method = {RequestMethod.POST, RequestMethod.PUT})
    public ResponseMsg<Client> updateClient(@RequestBody Client client) {
        return service.updateClient(client);
    }

    @ApiOperation(value = "查询客户端列表", notes = "")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseMsg<List<Client>> queryClients() {
        return service.queryClients();
    }

    @ApiOperation(value = "删除客户端信息", notes = "")
    @ApiImplicitParam(name = "clientId", value = "删除客户端的指定客户端Id", dataType = "String", required = true)
    @ResponseBody
    @RequestMapping(value = "/delete", method = {RequestMethod.DELETE,RequestMethod.GET})
    public ResponseMsg<Client> deleteClient(@RequestBody String clientId) {
        return service.deleteClient(clientId);
    }
}
